package cn.kent.fixsidetouch;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private CheckBox mCheckBox;
    private TextView mCurrent;
    private EditText mEditText;
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCheckBox = findViewById(R.id.side_bar_enabled);
        mCurrent = findViewById(R.id.side_bar_current);
        mEditText = findViewById(R.id.side_bar_et);
        mButton = findViewById(R.id.submit);
        updateView();
        setListener();
    }

    private void updateView() {
        boolean enabled = PrefUtils.getInstance().getSideBarEnabled();
        int percent = PrefUtils.getInstance().getSideBarPercent();
        String percentStr = "" + percent;
        String currentStr = "current:" + percent + "%";
        mCheckBox.setChecked(enabled);
        if (TextUtils.isEmpty(mEditText.getText())) {
            mEditText.setText(percentStr);
        }
        mCurrent.setText(currentStr);
        SideBar.getInstance().updateSideBar();
    }

    @Override
    protected void onResume() {
        super.onResume();
        SideBar.getInstance().showColor(true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        SideBar.getInstance().showColor(false);
    }

    private void setListener() {
        mCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            PrefUtils.getInstance().setSideBarEnabled(isChecked);
            updateView();
        });
        mButton.setOnClickListener(v -> {
            Context context = MainActivity.this;
            SideBar.getInstance().hideSideBar();
            int percent = 0;
            try {
                percent = Integer.parseInt(mEditText.getText().toString());
            } catch (Exception ignore) {
            }
            if (percent <= 0) {
                Toast.makeText(context, "input error", Toast.LENGTH_SHORT).show();
                return;
            }
            PrefUtils.getInstance().setSideBarPercent(percent);
            updateView();
        });
    }
}